require('dotenv').config();
const express = require('express');
const cors = require('cors');
const db = require('./config/db');
const { logger } = require('./utils/logger');


const app = express();
const port = process.env.PORT || 3000;

// 中间件
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(logger)

// 路由模板
app.use('/api/files', require('./routes/files'));
app.use('/api/media', require('./routes/media'));
app.use('/api/users', require('./routes/users'));
app.use('/duoduo', require('./routes/dusers.js'));
app.use('/duoduo/user', require('./routes/dusers.js'));
app.use('/duoduo/excel', require('./routes/excelImport.js'));

// app.use('/api/qiniuStorage', require('./utils/qiniuStorage')); // 注册路由，使用七牛云存储

// 错误处理中间件
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send('服务器错误！');
});

app.listen(port, () => {
  console.log(`服务器运行在 http://localhost:${port}`);
}); 